#ifndef _DFS_MSG
#define _DFS_MSG
#include <dfs.h>

/* Definitions */
typedef enum message_kind {
	DATA_KIND = 0,
	COMD_KIND = 1,
	REPL_KIND = 2
}message_kind;

typedef enum message_type {
	/* Reply messages (REPL_KIND) */
	RR_MSG		 = -3,
	OK_MSG		 = -1, /* OK msg */
	ER_MSG		 = -2, /* ERROR msg */
	/* Raw data message (DATA_KIND) */
	DATA_MSG	 =  0, /* Message contains raw data */
	/* DFS handles (COMD_KIND)*/
	OP_FILE_MSG  =  1, /* Open file request */
	RD_FILE_MSG  =  2, /* Read file request */
	WR_FILE_MSG  =  3, /* write file request */
	CL_FILE_MSG  =  4, /* close file request */
	/* Administration messages (COMD_KIND)*/
	CON_PEER_MSG  =  5, /* Connect to a Peer server request */
	DIS_PEER_MSG  =  6, /* Disconnect peers request */
	UPD_CACHE_MSG =  7, /* Update server's cache */
	CLR_CACHE_MSG =  8, /* Clear server's cache */
	GET_LOG_MSG   =  9, /* Get logs from server */
	GET_NEI_MSG   = 10, /* Get server's neighbouring peers*/
	GET_SFI_MSG   = 11, /* add file to server's sharing list 	  */
	REM_SFI_MSG   = 12,  /* remove file from server's sharing list */
	LIS_SFI_MSG   = 13, /* Get list of file in server's /home folder */
	/* FIXME : more messages */
	UNKNOWN_MSG   = 99 /* Unknown Type of message */
}message_type;

#define DATA_SIZE (BUFFER_SIZE - sizeof(MAXIPADDRESSLEN)*3 - sizeof(message_kind) -sizeof(message_type)- sizeof(char)*4)
/* 	Client on 192.168.54.1 connected on server 192.168.54.130 wants to OPEN (1) :
	Message  : 192.168.54.1|192.168.54.130|1|0[foo.txt */

typedef struct {
	char sender[MAXIPADDRESSLEN];
	char receiver[MAXIPADDRESSLEN];
	message_type type;
	message_kind kind; 
	char data[DATA_SIZE];
}dfs_message;

/* Function prototypes */
_PROTOTYPE(int build_message,(dfs_message *,const char *,const char *,int,int,const char *) );
_PROTOTYPE(int message_to_string,(dfs_message *,char *)			  							);
_PROTOTYPE(int string_to_message,(dfs_message *,char *)				  						);
/* These function prototypes act as wrappers for common Client-Server or Server-Server interacction */
_PROTOTYPE(void make_OK_reply_message,(dfs_message *,const char *)							);
_PROTOTYPE(void make_ER_reply_message,(dfs_message *,const char *)							);
_PROTOTYPE(void make_UNKNOWN_reply_message,(dfs_message *,const char *)						);
_PROTOTYPE(void make_RREQUEST_reply_message,(dfs_message *,const char *,const char *)		);
_PROTOTYPE(void reply_with_OK_message,(dfs_message *,const char *,char *,int)				);
_PROTOTYPE(void reply_with_ER_message,(dfs_message *,const char *,char *,int)				);
_PROTOTYPE(void reply_with_UNKNOWN_message,(dfs_message *,const char *,char *,int)			);
_PROTOTYPE(void reply_with_RRESPONCE_message,(dfs_message *,const char *,char *,int)		);
_PROTOTYPE(int handle_in_message,(dfs_message *m,char *,int)								);
#endif
